﻿using Microsoft.AspNetCore.Mvc;
using WebAPI.Dao;
using WebAPI.Entities;
using WebAPI.Services;

namespace WebAPI.Controllers;

[ApiController]
[Route("api/[controller]/[Action]")]
public class ClassifyController : ControllerBase
{
    private ClassifyService _classifyService = new ClassifyService();
    
    [HttpPost(Name = "AddClassify")]
    public string AddClassify(Classify classify)
    {
        bool flag = _classifyService.AddClassify(classify);
        if (!flag)
        {
            return ResultData.Fail("发布文章失败，请稍后重试");
        }
        return ResultData.Ok("发布成功");
    }
    
    [HttpGet(Name = "GetClassifies")]
    public string GetClassifies()
    {
        List<Classify> classifies = _classifyService.GetAllClassifies();
        if (classifies.Count == 0)
        {
            return ResultData.Ok("还未有分类，请稍后重试");
        }
        return ResultData.Ok(classifies);
    }
    
    [HttpPut(Name = "DeleteClassify")]
    public string DeleteClassify(Classify classify)
    {
        bool flag = _classifyService.DeleteClassify(classify);
        if (!flag)
        {
            return ResultData.Fail("删除文章失败，请稍后重试");
        }
        return ResultData.Ok("删除分类失败");
    }
    
    [HttpPut(Name = "UpdateClassify")]
    public ActionResult UpdateClassify(Classify classify)
    {
        bool flag = _classifyService.UpdateClassify(classify);
        if (!flag)
        {
            return BadRequest("更新文章失败，请稍后重试");
        }
        return Ok();
    }
}